package cn.wowjoy.pda.android_pharmacy.ui.activity.hospital.put;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;

import cn.wowjoy.pda.android_pharmacy.R;
import cn.wowjoy.pda.android_pharmacy.common.core.BaseActivity;
import cn.wowjoy.pda.android_pharmacy.common.utils.Contants;
import cn.wowjoy.pda.android_pharmacy.common.widget.MDialog;
import cn.wowjoy.pda.android_pharmacy.common.widget.MyToast;
import cn.wowjoy.pda.android_pharmacy.databinding.ActivityInHospitalPutProgressBinding;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.putInfo.PutSingleBeen;
import cn.wowjoy.pda.android_pharmacy.mvvm.presenter.InHospitalPutProgressPresenter;
import cn.wowjoy.pda.android_pharmacy.mvvm.vm.InHospitalPutProgressViewModel;

public class InHospitalPutProgressActivity extends BaseActivity<ActivityInHospitalPutProgressBinding,InHospitalPutProgressViewModel,InHospitalPutProgressPresenter > implements View.OnClickListener {
    private MDialog mDialog;
    PutSingleBeen topRemove ;

    //PDA扫码广播
    private ScanReceiver mScanReceiver;
    private IntentFilter intentFilter;
    public static final int CLOSE=1;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case CLOSE:
                    //TODO:自动跳转
//                    if(null != mDialog){
//                        mDialog.cancel();
//                    }
                    Intent intent=new Intent();
                    intent.putExtra("check",topRemove);
                    setResult(Contants.RESULT_SUCCESS_CODE,intent);
                    finish();
                    break;
            }
        }
    };

    @Override
    protected void init(Bundle savedInstanceState) {
        binding.setPresenter(presenter);
        binding.setViweModel(viewModel);
        binding.scanGetTitle.titleTextHintTv.setText("药品上架");
        binding.scanGetTitle.titleBackLl.setVisibility(View.VISIBLE);
        binding.scanGetTitle.titleBackTv.setText("上架单详情");
        binding.scanGetTitle.titleBackLl.setOnClickListener(this);
        binding.btSureSuccess.setOnClickListener(this);
        mScanReceiver = new ScanReceiver();
        intentFilter = new IntentFilter();
        intentFilter.addAction(Contants.WOW_NEW_ADD);
//        binding.numFactDoorScanResult.setFocusable(true);
//        binding.numFactDoorScanResult.setFocusableInTouchMode(true);
        binding.numFactDoorScanResult.requestFocus();
        //TODO:界面信息的更细
        topRemove = (PutSingleBeen) getIntent().getExtras().getSerializable("check");
        if(null != topRemove){
//            updateUI(topRemove);
            binding.setModel(topRemove);
            binding.medicineNameScanGet.setText(topRemove.getZZYPMC()+"   "+topRemove.getZZYPGG());
            binding.numScanGet.setText(Math.abs(Integer.parseInt(topRemove.getCFYPSL()))+"");
            binding.numFactDoorScanResult.setText(Math.abs(Integer.parseInt(topRemove.getCFYPSL()))+"");
            binding.numFactDoorScanResult.setSelection(binding.numFactDoorScanResult.getText().length());
            if( null != topRemove.getKFWZMC() && topRemove.getKFWZMC().length()>=7){
                binding.locatorScanGet.setText(topRemove.getKFWZMC().substring(0,7));
            }else{
                binding.locatorScanGet.setText(topRemove.getKFWZMC());
            }
        }
        //TODO:应取数量和取药数量的对比
        binding.btSureSuccess.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (binding.numFactDoorScanResult.getText().length() == 0) {
                    new MyToast(InHospitalPutProgressActivity.this).showinfo("请输入取药数量");
                } else {
                    if (!presenter.check(binding.numScanGet.getText().toString().trim(), binding.numFactDoorScanResult.getText().toString().trim())){
                        new MyToast(InHospitalPutProgressActivity.this).showinfo("请核对数量");
                        binding.numFactDoorScanResult.setText("");
                        return;
                    }
                    if("".equals(binding.tvScanGetScan.getText().toString().trim())){
                        new MyToast(InHospitalPutProgressActivity.this).showinfo("请扫描库位条形码");
                        return;
                    }
                    if(binding.tvScanGetScan.getText().toString().trim().length()<=7 ){
                        if (!binding.tvScanGetScan.getText().toString().trim().equals(binding.locatorScanGet.getText().toString().trim())){
                            new MyToast(InHospitalPutProgressActivity.this).showinfo("药品上架位置不正确,请手动输入库位");
                            return;
                        }
                    }else if( !binding.tvScanGetScan.getText().toString().trim().equals(binding.locatorScanGet.getText().toString().trim().substring(0,7))){
                        new MyToast(InHospitalPutProgressActivity.this).showinfo("药品上架位置不正确,请重新核对");
                        return;
                    }
                    //TODO:弹框 药品Name
//                    mDialog = CreateDialog.successDialog(InHospitalPutProgressActivity.this,6 ,"上架成功");
                    mHandler.sendEmptyMessage(CLOSE);
                }
            }
        });
    }
    @Override
    public void onBackPressed() {
        Intent intent=new Intent();
        intent.putExtra("check",topRemove);
        setResult(Contants.RESULT_FAIL_CODE ,intent);
        super.onBackPressed();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_in_hospital_put_progress;
    }

    @Override
    protected Class<InHospitalPutProgressPresenter> getPresenterClass() {
        return InHospitalPutProgressPresenter.class;
    }

    @Override
    protected Class<InHospitalPutProgressViewModel> getViewModelClass() {
        return InHospitalPutProgressViewModel.class;
    }
    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver(mScanReceiver, intentFilter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(mScanReceiver);
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case  R.id.title_back_ll:
                setResult(Contants.RESULT_FAIL_CODE);
                finish();
                break;
        }
    }
    //TODO:接收PDA扫码广播，准备跳转
    class ScanReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Contants.WOW_NEW_ADD)) {
                Log.e("TAG", "onReceive: "+intent.getStringExtra(Contants.WOW_NEW_KEY) );
                //要做下扫码的过滤判断
                String stringExtra = intent.getStringExtra(Contants.WOW_NEW_KEY);
                binding.tvScanGetScan.setText(stringExtra);
            }
        }
    }
}
